//
//  HeroSegmentView.m
//  首页
//
//  Created by Hero on 15/12/28.
//  Copyright © 2015年 Hero. All rights reserved.
//

#import "HeroSegmentView.h"
#import "UIViewAdditions.h"

@interface HeroSegmentView()<UIScrollViewDelegate>
@property (nonatomic,weak) UIScrollView *contentScrollView;
@property (nonatomic,weak) UIView *titleView;
//父控制器
@property (nonatomic,weak) UIViewController *parentViewController;
/** 标题按钮底部的指示器 */
@property (nonatomic, weak) UIView *indicatorView;
@property (nonatomic,strong) NSArray *viewControllers;
@property (nonatomic, weak) UIButton *selectedButton;
@end

@implementation HeroSegmentView

- (void)setViewControllers:(NSArray *)viewControllers owner:(UIViewController *)parentViewController
{
    _viewControllers = viewControllers;
    self.parentViewController = parentViewController;
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    
    contentScrollView.pagingEnabled = YES;
    contentScrollView.showsVerticalScrollIndicator = NO;
    contentScrollView.showsHorizontalScrollIndicator = NO;
    contentScrollView.bounces = NO;
    contentScrollView.delegate = self;
    
    [self addSubview:contentScrollView];
    self.contentScrollView = contentScrollView;
    
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *vc = viewControllers[i];
        [self.parentViewController addChildViewController:vc];
    }
    _contentScrollView.contentSize = CGSizeMake(_contentScrollView.frame.size.width * viewControllers.count, _contentScrollView.frame.size.height);
    
    [self addChildVcView];
}

- (void)setScrollViewCanScoroll:(BOOL)scrollViewCanScoroll{
    self.contentScrollView.scrollEnabled = scrollViewCanScoroll;
}

//头部的导航视图
-(UIView *)topControlViewWithFrame:(CGRect)frame defaultColor:(UIColor *)defaultColor selectedColor:(UIColor *)seleteColor
{
    UIView *titleView = [[UIView alloc] initWithFrame:frame];
    _btnArray = [NSMutableArray array];
    
    for (int i = 0; i < _viewControllers.count; i++) {
        CGFloat space = 12;
        UIViewController *vc = _viewControllers[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:defaultColor forState:UIControlStateNormal];
        [btn setTitleColor:seleteColor forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        [btn setTitle:vc.title forState:UIControlStateNormal];
        CGFloat width = 70;
        btn.frame = CGRectMake(i*width+space*(i+1), 0, width, frame.size.height);
        
        [titleView addSubview:btn];

        [_btnArray addObject:btn];
    }
    UIButton *firstBtn = [_btnArray firstObject];
    //立刻根据文字内容计算label的宽度，这句必须，不加的话，第一次进入界面时下边的下划线消失
    [firstBtn.titleLabel sizeToFit];
    firstBtn.selected = YES;
    
    UIView *indicatorView = [[UIView alloc] init];
    
    indicatorView.backgroundColor =UIColorFromRGB(0x1A7BE5);
    indicatorView.height = 1;
    indicatorView.top = titleView.height - indicatorView.height;
    indicatorView.width = 25;
    indicatorView.centerX = firstBtn.centerX;
    [titleView addSubview:indicatorView];
    
    self.indicatorView = indicatorView;
    self.titleView = titleView;
    
    //默认选中第一个按钮
    // 默认情况 : 选中最前面的标题按钮
    firstBtn.selected = YES;
    self.selectedButton = firstBtn;
    
    return titleView;
}


- (void)btnClick:(UIButton *)btn{
    self.selectedButton.selected = NO;
    btn.selected = YES;
    self.selectedButton = btn;
    
    // 指示器动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = 25;
        self.indicatorView.centerX = btn.centerX;
    }];
    
    [self contentScrollViewShowPage:btn.tag-100];
}

- (void)addChildVcView
{
    // 子控制器的索引
    NSUInteger index = _contentScrollView.contentOffset.x /  _contentScrollView.width;
    
    // 取出子控制器
    UIViewController *childVc = self.viewControllers[index];
    if ([childVc isViewLoaded]) return;
    childVc.view.frame = _contentScrollView.bounds;
    
    [_contentScrollView addSubview:childVc.view];
}

- (void)contentScrollViewShowPage:(NSInteger)page
{
    [_contentScrollView setContentOffset:CGPointMake(page * _contentScrollView.frame.size.width, 0) animated:YES];
}

//这个不能少  去掉点击头部按钮失效
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self addChildVcView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSUInteger index = scrollView.contentOffset.x / scrollView.width;
    UIButton *titleButton = self.titleView.subviews[index];
    [self btnClick:titleButton];
    [self addChildVcView];
}

@end
